package com.student.score.entity;

import java.util.ArrayList;
import java.util.List;

public class Student {
    private String id;       // 学号
    private String name;     // 姓名
    private String className;// 班级
    private List<Score> scores = new ArrayList<>(); // 学生的成绩列表

    // 构造函数
    public Student(String id, String name, String className) {
        this.id = id;
        this.name = name;
        this.className = className;
    }

    // 添加成绩
    public void addScore(Score score) {
        this.scores.add(score);
    }

    // 计算GPA（绩点规则：90+→4.0，80-89→3.0，70-79→2.0，60-69→1.0，<60→0）
    public double calculateGPA() {
        if (scores.isEmpty()) {
            return 0.0;
        }
        double totalPoint = 0.0;
        int totalCredit = 0;

        for (Score score : scores) {
            Course course = score.getCourse();
            int credit = course.getCredit();
            int scoreValue = score.getScore();
            double point;

            if (scoreValue >= 90) point = 4.0;
            else if (scoreValue >= 80) point = 3.0;
            else if (scoreValue >= 70) point = 2.0;
            else if (scoreValue >= 60) point = 1.0;
            else point = 0.0;

            totalPoint += point * credit;
            totalCredit += credit;
        }
        return totalPoint / totalCredit;
    }

    // 查询某课程的成绩
    public int queryCourseScore(String courseId) throws Exception {
        for (Score score : scores) {
            if (score.getCourse().getId().equals(courseId)) {
                return score.getScore();
            }
        }
        throw new Exception("该学生未选修此课程");
    }

    // Getter方法
    public String getId() { return id; }
    public String getName() { return name; }
    public String getClassName() { return className; }
    public List<Score> getScores() { return scores; }
}